import { BaseEvent } from "./base";

class SelectionEventer extends BaseEvent {

  addEventListen(): void {
    document.addEventListener('selectionchange', this._onSelectionChange.bind(this))
  }
  removeEventListen(): void {

  }
  _onSelectionChange(e: Event) {
    let selection = document.getSelection()
    if (selection) {
      if (selection.anchorOffset <= selection.focusOffset) {
        this.editor.updateSelection(selection.anchorOffset,selection.focusOffset - selection.anchorOffset)
      } else {
        this.editor.updateSelection(selection.focusOffset,selection.anchorOffset - selection.focusOffset)
      }
    }
  }
}

export default SelectionEventer